#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "peerlist.h"
#include "debug.h"


BOOL check_peer_by_name(list_t *pt_peer_list, char* name)
{
    int i;

    for(i=0; i<list_size(pt_peer_list); i++) {
        T_PeerInfo *peer = (T_PeerInfo *)list_get_at(pt_peer_list, i);
        
        if(peer) {
            if(strcmp(peer->name, name) == 0) {
                return TRUE;
            }
        }
    }
    
    return FALSE;
}

BOOL check_peer(list_t *pt_peer_list, T_PeerInfo *pt_peer)
{
    int i;

    for(i=0; i<list_size(pt_peer_list); i++)
    {
        T_PeerInfo *peer = (T_PeerInfo *)list_get_at(pt_peer_list, i);
        
        if(peer)
        {
            //P2P_DBG_DEBUG("local name:%s, name to add:%s", peer->name, pt_peer->name);
            if(strcmp(peer->name, pt_peer->name) == 0)
            {
                return TRUE;
            }
        }
    }
    
    return FALSE;
}

int add_peer(list_t *pt_peer_list, T_PeerInfo *pt_peer)
{
    if(NULL == pt_peer_list || NULL == pt_peer)
    {
        P2P_ERROR("add_peer failed, invalid parameter!");
        return -1;
    }

    if(check_peer(pt_peer_list, pt_peer))
    {
        P2P_DEBUG("peer exist!");
        return -1;
    }
    
    list_append(pt_peer_list, (void*)pt_peer);
    
    return 0;
}

int del_peer(list_t *pt_peer_list, T_PeerInfo *pt_peer)
{
    if(NULL == pt_peer_list || NULL == pt_peer)
    {
        P2P_ERROR("invalid parameter!");
        return -1;
    }

    int i;

    for(i=0; i<list_size(pt_peer_list); i++)
    {
        T_PeerInfo *peer = (T_PeerInfo *)list_get_at(pt_peer_list, i);       
        if(peer)
        {
            if(strcmp(peer->ID, pt_peer->ID) == 0)
            {
                list_delete(pt_peer_list, pt_peer);
            }
        }
    }
    
    return 0;
}

int del_peer_by_name(list_t *pt_peer_list, const char *name)
{
    if(NULL == pt_peer_list || NULL == name)
    {
        P2P_ERROR("invalid parameter!");
        return -1;
    }

    int i;

    for(i=0; i<list_size(pt_peer_list); i++)
    {
        T_PeerInfo *peer = (T_PeerInfo *)list_get_at(pt_peer_list, i);        
        if(peer)
        {
            if(strcmp(peer->name, name) == 0)
            {
                list_delete(pt_peer_list, peer);
            }
        }
    }
    
    return 0;
}

int get_peer_num(list_t *pt_peer_list)
{
    if(NULL == pt_peer_list)
    {
        P2P_ERROR("invalid parameter!");
        return 0;
    }

    return list_size(pt_peer_list);
}

T_PeerInfo *get_peer_by_name(list_t *pt_peer_list, char* name)
{
    int i;
    
    if(NULL == pt_peer_list)
    {
        P2P_ERROR("invalid parameter!");
        return NULL;
    }

    for(i=0; i<list_size(pt_peer_list); i++)
    {
        T_PeerInfo *peer = (T_PeerInfo *)list_get_at(pt_peer_list, i);        
        if(peer)
        {
            //printf("%s == %s\r\n", peer->name, name);
            if(strcmp(peer->name, name) == 0)
            {
                return peer;
            }
        }
    }

    //printf("faild!!!\r\n");
    return NULL;
}

T_PeerInfo *get_peer_by_idx(list_t *pt_peer_list, int idx)
{
    if(NULL == pt_peer_list)
    {
        P2P_ERROR("invalid parameter!");
        return NULL;
    }

    return (T_PeerInfo*)list_get_at(pt_peer_list, idx);
}

size_t szelem(const void *el) {
    return sizeof(T_PeerInfo);
}

int init_peer_list(list_t *pt_peer_list)
{
    list_init(pt_peer_list);
    list_attributes_copy(pt_peer_list, szelem, 1);
    
    return 0;
}

int clear_peer_list(list_t *pt_peer_list)
{
    if(NULL == pt_peer_list)
    {
        P2P_ERROR("invalid parameter!");
        return -1;
    }

    list_clear(pt_peer_list);
    
    return 0;
}

int reset_peer_heart_value_by_name(list_t *pt_peer_list, char* name)
{
    if(!pt_peer_list || !name) {
        P2P_ERROR("invalid parameter!");
        return -1;
    }

    int i;

    for(i=0; i<get_peer_num(pt_peer_list); i++) {
        T_PeerInfo *peer = (T_PeerInfo *)list_get_at(pt_peer_list, i);
        if(peer) {
            if(strcmp(peer->name, name) == 0) {
                T_PeerInfo *tmp = (T_PeerInfo*)malloc(sizeof(T_PeerInfo)); 
                memcpy(tmp, peer, sizeof(T_PeerInfo));
                tmp->heart_fail_count = 0;
                list_delete(pt_peer_list, peer);
                list_append(pt_peer_list, tmp);
                free(tmp);
            }
        }
    }
    
    return 0;
}

int peer_list_uinit(list_t *pt_peer_list)
{
    if(NULL == pt_peer_list)
    {
        P2P_ERROR("invalid parameter!");
        return -1;
    }

    list_destroy(pt_peer_list);
    
    return 0;
}

